package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.CommonListActivity;
import com.xmut.bs.handaccount.bmob.Article;
import com.xmut.bs.handaccount.bmob.Collection;
import com.xmut.bs.handaccount.util.DisplayUtils;
import com.xmut.bs.handaccount.util.DividerItemDecoration;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

import static com.xmut.bs.handaccount.base.Constants.PARAM_ARTICLE_ID;
import static com.xmut.bs.handaccount.base.Constants.PARAM_EDIT;

/**
 * Description
 * Created by Dollkey on 2018/4/22.
 */

public class ArticleCollectionActivity extends CommonListActivity<Article> {

    private Collection mCollection;

    @Override
    protected String getSubTitle() {
        return mCollection.getName();
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new DividerItemDecoration(mContext,
                DisplayUtils.dp2px(mContext, 8),
                getResources().getColor(R.color.grey100)).setHasLeftOrRight(false).setHasTopAndBottom(false);
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getRvLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected RecyclerView.Adapter getAdapter(List<Article> comList) {
        return new CommonAdapter<Article>(mContext, R.layout.item_article, comList) {
            @Override
            protected void convert(ViewHolder holder, final Article article, int position) {
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent().putExtra(PARAM_ARTICLE_ID, article), ArticleDetailActivity.class);
                    }
                });
                holder.setText(R.id.goods_name, article.getTitle());
                holder.setText(R.id.username, article.getUser().getNickName());
                holder.setVisible(R.id.start_video_img, article.getContainVideo());
                ImageView coverImg = holder.getView(R.id.cover_img);
                ImageView avatarImg = holder.getView(R.id.avatar_img);
                GlideApp.with(coverImg).load(article.getCoverImg()).override(1200, 600).centerCrop().into(coverImg);
                GlideApp.with(coverImg).load(article.getUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);
            }
        };
    }

    @Override
    public void getData(int pageNo, int pageSize) {
        if (pageNo != 0) return;
        mBmobQuery.addWhereRelatedTo("article", new BmobPointer(mCollection));
        mBmobQuery.include("user");
        mBmobQuery.findObjects(new FindListener<Article>() {
            @Override
            public void done(List<Article> list, BmobException e) {
                if (e == null) {
                    showList(list);
                } else {
                    showError("请求失败：" + e.getMessage());
                }
            }
        });
    }

    @Override
    protected void initParams(Intent intent) {
        mCollection = (Collection) intent.getSerializableExtra(PARAM_EDIT);
    }
}
